var page_fullscreen = {
	tr: null,
	currentItem:0,
    effect: null,
    fullscreen : null,
    full_screen_showing : false,
    img_size_table:{},
    init: function() {
	    // 处理全屏效果
	    page_fullscreen.fullScreen = $('#fullScreen').fullScreen();
		page_fullscreen.initSizeTable();
	    $(page_fullscreen.fullScreen).bind('fullscreenShown', function() {
       		page_fullscreen.full_screen_showing = true;
       		page_fullscreen.tr = $("#gallery").fadeTransition({pauseTime: 700000000, transitionTime: 1500,normal:'2'});
			page_fullscreen.updateLayout();
       });
      $(page_fullscreen.fullScreen).bind('fullscreenHidden', function() {
   			 page_fullscreen.full_screen_showing = false;

      }); 
      $(window).bind('resize', function(evt) {
      		if(page_fullscreen.full_screen_showing)
            	page_fullscreen.updateLayout();
      });
    
	 //page_fullscreen.tr = $("#gallery").fadeTransition({pauseTime: 700000000, transitionTime: 1500,normal:'0'});
	  $("#fullScreen .navicontroller a").each(function(idx) {
	  		page_fullscreen.totalItem++;
			$(this).click(function() {
				page_fullscreen.tr.showItem(idx); 
				page_fullscreen.currentItem = idx;
				return false;
				}
			)
	  });
	$(page_fullscreen.tr).bind('fadeTransitiondoing',function(){
		page_fullscreen.updateLayout();
	});
  },
  initSizeTable:function(){
  	  page_fullscreen.fullScreen.gallery.find('.photo-b img').each(function(_idx) {
  	  	$($(this)[0]).css('position','absolute');
  	  	if($(this)[0].width > 0 && $(this)[0].height)
  	  	{
      	  var img_size = {'width' : $(this)[0].width, 'height' : $(this)[0].height};
       	 	page_fullscreen.img_size_table[_idx] = img_size;
        }
    });
  },
  updateLayout : function() {
 		page_fullscreen.locateBigImage();
 		page_fullscreen.locateNavigtionBar();
  },
  locateBigImage:function(){
  	  /*********** locate big image begin**********/
     $('#gallery img').each(function(_idx){
     		var css_display = $(this).css('display');
     		if(css_display != 'none')
     		{
     		//	var imgObj = $(this).find('img').get(0);
     			var imgObj = $(this);
     			//$(imgObj).show();
     			if( page_fullscreen.img_size_table[_idx] == undefined)
     			{
     				 var img_size = {'width' : imgObj.width(), 'height' : imgObj.height()};
     				 page_fullscreen.img_size_table[_idx] = img_size;
     			}
     			var img_original_size = page_fullscreen.img_size_table[_idx];
     			var window_width = $(window).width();
     			var window_height = $(window).height();
     			
     			var wn_padding_top = 30;
     			var wn_padding_bottom = 120;
     			var wn_padding_left = 10;
     			var wn_padding_right = 10;
     			
     			window_width = window_width - wn_padding_left - wn_padding_right;
     			window_height = window_height - wn_padding_top - wn_padding_bottom;
     			
     			var w_scale = window_width/(img_original_size.width);
     			var h_scale = window_height/(img_original_size.height);
     			var scale = w_scale;
     			if(h_scale < scale)
     				scale = h_scale;
     			var resize_w = img_original_size.width * scale;
     			var resize_h = img_original_size.height*scale;
     			//$($(this).find('img').get(0)).attr().attr();
     			
     			 imgObj.css('width',resize_w+'px');
     			 imgObj.css('height',resize_h+'px');
     			 $.log('iw:'+resize_w);
     			 $.log('ih:'+resize_h);
     			 
     			// $(imgObj).parent().css('height',resize_h+'px');
     			 $(imgObj).css('left',(window_width - resize_w )/2 + wn_padding_left +'px');
     			 $(imgObj).css('top',(window_height - resize_h )/2+wn_padding_top+'px');
     			 //$.log('margin-left'+(window_width - resize_w )/2 + wn_padding_left);
     			// $.log('margin-top:'+(window_height - resize_h )/2+wn_padding_top);
     			//return true;
     		}
     });
  },
  locateNavigtionBar:function(){
         /*********** locate big image end **********/
       
                       /*-----TOM ADD --location navigation bar ------begin*/
   				 var navigationbarObj = $('#fullScreen #photo-page2');
   				 var widow_height = $(window).height();
   				 var window_width = $(window).width();
   				 
   				 $("#fullScreen .photo-page2-2").parent().css('width',window_width+'px');
   				 
   				 navigationbarObj.css('margin-top',(widow_height - 120)+'px');
   				 
   				 var navicontroller_bar_item_count = $('#fullScreen .navicontroller a').size();
   				
				  var navicontroller_bar_size = navicontroller_bar_item_count * 91;
   				 var navigationbar_with = navicontroller_bar_size;
   				 var navigationbar_container_width = window_width - 200;
   				
				navigationbar_with = navigationbar_container_width - 150;
		
				
				 
				 $("#fullScreen .photo-page2-2").css('width', navigationbar_with+'px');
				 // $("#fullScreen .photo-page2-2").parent().css('width',navigationbar_container_width +'px');
	 			var margin_left = (navigationbar_container_width - navigationbar_with)/2;

				 $("#fullScreen .photo-page2-1").css("margin-left",margin_left+'px');
	 			/*-----TOM ADD --location navigation bar ------end*/
  },
  start: function() {
  
     //this.updateLayout();
     page_fullscreen.fullScreen.show();
     var navWidth = page_fullscreen.fullScreen.gallery.find('.photo-page2-2-bg').outerWidth(true) * page_fullscreen.fullScreen.gallery.find('.photo-page2-2-bg').length;

     page_fullscreen.fullScreen.gallery.find('a.navicontroller_prev').click(function() {
        var _width = page_fullscreen.fullScreen.gallery.find('.photo-page2-2-bg').eq(0).outerWidth(true); 
        var _strMarginLeft = page_fullscreen.fullScreen.gallery.find('.navicontroller').css('margin-left');
        var _marginLeft = parseInt(_strMarginLeft.substring(0, _strMarginLeft.indexOf('px')));
        if (_marginLeft + _width <= 0) {
            page_fullscreen.fullScreen.gallery.find('.navicontroller').css('margin-left', _marginLeft + _width); 
        }
     });
     page_fullscreen.fullScreen.gallery.find('a.navicontroller_next').click(function() {
     
        var _width = page_fullscreen.fullScreen.gallery.find('.photo-page2-2-bg').eq(0).outerWidth(true);
        
        var _strMarginLeft = page_fullscreen.fullScreen.gallery.find('.navicontroller').css('margin-left');
        var _marginLeft = parseInt(_strMarginLeft.substring(0, _strMarginLeft.indexOf('px')));
        var _strMaxWidth = page_fullscreen.fullScreen.gallery.find('.photo-page2-2').css('width');
        var _maxWidth = parseInt(_strMaxWidth.substring(0, _strMaxWidth.indexOf('px')));
        if ( navWidth  + _marginLeft > _maxWidth) {
           page_fullscreen.fullScreen.gallery.find('.navicontroller').css('margin-left', _marginLeft - _width); 
        }
     });
  },
  end: function() {
     page_fullscreen.fullScreen.hide();
     //page_fullscreen.effect.stop();
     page_fullscreen.fullScreen.gallery.find('.photo-page2-2-list a').each(function(_idx){
         $(this).unbind('click');
     });

     page_fullscreen.fullScreen.gallery.find('a.navicontroller_prev').unbind('click');
     page_fullscreen.fullScreen.gallery.find('a.navicontroller_prev').unbind('click');

  }
};
